package com.dhcc.bpm.modules.mail.vo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.util.Set;

@Slf4j
@Component
public class RequesterCredentials implements SOAPHandler<SOAPMessageContext> {

//    @Value("${msg.userName}")
    private String _userName = "PM02";;

//    @Value("${msg.password}")
    private String _password = "a25b73d1-4fbf-401c-ab30-db656bf414ee";;

    @Override
    public Set<QName> getHeaders() {





        return null;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outboundProperty = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty) {
            SOAPMessage message = context.getMessage();
            try {
                SOAPHeader header = message.getSOAPHeader();
                if (header == null) {
                    message.getSOAPPart().getEnvelope().addHeader();
                    header = message.getSOAPHeader();
                }
                SOAPElement authenticationToken = header.addChildElement("Security","wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                SOAPElement usernameToken = authenticationToken.addChildElement("UsernameToken","wsse");
                usernameToken.addChildElement("Username","wsse").addTextNode(_userName);
                usernameToken.addChildElement("Password","wsse").addTextNode(_password);
            } catch (Exception e) {
                log.error("异常信息：", e);
            }
        }
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    @Override
    public void close(MessageContext context) {

    }
}
